<?php
class wd_post_category_widget extends WP_Widget
{  
    public function wd_post_category_widget()
    {  
      $widgetOptions = array( 'description' => 'Pozwala na wyświetlenie linków do wpisów z podanej kategorii');
      parent::WP_Widget(false, __('WD Post category'), $widgetOptions);  
    }  
    
    public function form($instance)
    {       
        $formWrapper = '<p><label for="%1$s">%2$s</label><input class="widefat" id="%1$s" name="%3$s" type="text" value="%4$s" /></p>';
        
        $title = sprintf($formWrapper, $this->get_field_id('title'), __('Title:'), $this->get_field_name('title'), esc_attr($instance['title']));
        
        $metaName = sprintf($formWrapper, $this->get_field_id('name'), __('Category:'), $this->get_field_name('name'), esc_attr($instance['name']));
              
        print $title.$metaName;
    }
    
    public function update($new_instance, $old_instance)
    {  
        return $new_instance;  
    }
    
    public function widget($args, $instance)
    {  
        $args['title'] = $instance['title'];  
        
        $args['name'] = $instance['name'];  
                
        self::display($args['name'],  $args['title']);
    }  
    
    
    public static function display($catgory, $title = null)
    {       
      if(!is_null($title))
      {
        printf('<div class="widget widget_pp_post_category"><h3 class="widget-title">%s</h3>', $title);
      }    
      
      
      if(!is_null($catgory) && $catgory != '')
      {
        query_posts( 'category_name=' . $catgory);

        print '<ul>';
        
        while ( have_posts() )
        {
          the_post();
          global $post;
          
          printf('<li><a href="%s">%s</a></li>', get_permalink( $post->ID ), $post->post_title);
        
        }
        
        print '</ul>';
        
        // bardzo ważne! inaczej mogą pojawić się dziwaczne błędy związane z użyciem the_post()
        wp_reset_query();        
      }
      
      if($title != null)
      {
        print '</div>';
      }
    } 
    
    private static function current_page_url()
    {
	$pageURL = 'http';
        
	if( isset($_SERVER["HTTPS"]) )
        {
		if ($_SERVER["HTTPS"] == "on")
                {
                  $pageURL .= "s";                  
                }
	}
	$pageURL .= "://";
        
	if ($_SERVER["SERVER_PORT"] != "80")
        {
          $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
	}
        else
        {
          $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
	}
        
	return $pageURL;
    }
    
}  

register_widget('wd_post_category_widget');  
?>